package com.example.jikeyoujikeyou.office_sz.view.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.model.Department;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditDepartmentActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditEnterpriseAddressActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditEnterpriseNameActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditTelephoneActivity;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseFragment;

public class VCompanyFragment extends BaseFragment {

    private View activity_fill_info;
    private LinearLayout enterprise_name_linearLayout,enterprise_telephone_linearLayout,enterprise_address_linearLayout,enterprise_size_linearLayout,create_department_linearLayout;
    private TextView tv_enterprise_name,tv_enterprise_telephone,tv_enterprise_address,tv_enterprise_department;
    private Spinner tv_enterprise_scale;
    private Button fill_company_info_next;
    private ListView lv_demartments;
    private DepartmentAdapter departmentAdapter;

    private String SORT = "...";

    private static final int DEPARTMENT_NAME_CODE = 100;
    private static final int DEPARTMENT_TELEPHONE_CODE = 200;
    private static final int DEPARTMENT_ADDRESS_CODE = 300;
    private static final int CREATE_DEPARTMENT_CODE = 500;

    private Handler reviseDepartment_name = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.arg1 > 0) {
                tv_enterprise_department.setText(msg.arg1+"");
                tv_enterprise_department.setTextColor(getResources().getColor(R.color.red));
            } else {
                tv_enterprise_department.setText("必填信息");
                tv_enterprise_department.setTextColor(getResources().getColor(R.color.grey));
            }
            hasNextCondition();
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case DEPARTMENT_NAME_CODE:
                final String enterprise_name = data.getStringExtra("enterprise_name");
                Handler reviseEnterprise_name = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        tv_enterprise_name.setText(enterprise_name.length() > 8 ? enterprise_name.substring(0,8) + SORT : enterprise_name);
                    }
                };
                reviseEnterprise_name.sendMessage(reviseEnterprise_name.obtainMessage());
                hasNextCondition();
                break;
            case DEPARTMENT_TELEPHONE_CODE:
                final String enterprise_telephone = data.getStringExtra("enterprise_telephone");
                Handler reviseEnterprise_telephone = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        tv_enterprise_telephone.setText(enterprise_telephone);
                    }
                };
                reviseEnterprise_telephone.sendMessage(reviseEnterprise_telephone.obtainMessage());
                hasNextCondition();
                break;
            case DEPARTMENT_ADDRESS_CODE:
                final String enterprise_address = data.getStringExtra("enterprise_address");
                Handler reviseEnterprise_address = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        RegisterActivity.company.setAddress(enterprise_address);
                        tv_enterprise_address.setText(enterprise_address.length() > 8 ? enterprise_address.substring(0,8) + SORT : enterprise_address);
                    }
                };
                reviseEnterprise_address.sendMessage(reviseEnterprise_address.obtainMessage());
                hasNextCondition();
                break;
            case CREATE_DEPARTMENT_CODE:
                String department_name = data.getStringExtra("department_name");
                System.out.println("department_name:"+department_name);
                Department department = new Department();
                department.setName(department_name);
                departmentAdapter.addItem(department);
                Message message = reviseDepartment_name.obtainMessage();
                message.arg1 = departmentAdapter.getDepartmentList().size();
                reviseDepartment_name.sendMessage(message);
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity_fill_info = inflater.inflate(R.layout.activity_fill_info,null,false);
        init();
        return activity_fill_info;
    }

    private void init() {
        enterprise_name_linearLayout = activity_fill_info.findViewById(R.id.enterprise_name_linearLayout);
        enterprise_telephone_linearLayout = activity_fill_info.findViewById(R.id.enterprise_telephone_linearLayout);
        enterprise_address_linearLayout = activity_fill_info.findViewById(R.id.enterprise_address_linearLayout);
        enterprise_size_linearLayout = activity_fill_info.findViewById(R.id.enterprise_size_linearLayout);
        create_department_linearLayout = activity_fill_info.findViewById(R.id.create_department_linearLayout);

        tv_enterprise_name = activity_fill_info.findViewById(R.id.tv_enterprise_name);
        tv_enterprise_telephone = activity_fill_info.findViewById(R.id.tv_enterprise_telephone);
        tv_enterprise_address = activity_fill_info.findViewById(R.id.tv_enterprise_address);
        tv_enterprise_scale = activity_fill_info.findViewById(R.id.tv_enterprise_scale);
        tv_enterprise_department = activity_fill_info.findViewById(R.id.tv_enterprise_department);

        fill_company_info_next = activity_fill_info.findViewById(R.id.fill_company_info_next);
        lv_demartments = activity_fill_info.findViewById(R.id.lv_demartments);
        departmentAdapter = new DepartmentAdapter(getContext(),reviseDepartment_name);
        lv_demartments.setAdapter(departmentAdapter);

        addOnClickListener();

    }

    private void hasNextCondition(){

        String name = tv_enterprise_name.getText().toString();
        String telphone = tv_enterprise_telephone.getText().toString();
        String address = tv_enterprise_address.getText().toString();
        int scale_status = tv_enterprise_scale.getSelectedItemPosition();

        System.out.println("name:"+name);
        System.out.println("telphone:"+telphone);
        System.out.println("address:"+address);
        System.out.println("scale:"+scale_status);

        if (!isEmpty(name) && !isEmpty(telphone) && !isEmpty(address)  && !(departmentAdapter.getDepartmentList().size() <= 0)) {
            fill_company_info_next.setEnabled(true);
        } else {
            fill_company_info_next.setEnabled(false);
        }
    }

    private boolean isEmpty(String str){
        if (str == null || str.trim().equals("") ) {
            return true;
        }
        return false;
    }

    private void addOnClickListener(){
        enterprise_name_linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(),EditEnterpriseNameActivity.class),1);
            }
        });
        enterprise_telephone_linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(),EditTelephoneActivity.class),1);
            }
        });
        create_department_linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(), EditDepartmentActivity.class),1);
            }
        });
        enterprise_address_linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(), EditEnterpriseAddressActivity.class),1);
            }
        });
        fill_company_info_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.company.setName(tv_enterprise_name.getText().toString());
                RegisterActivity.company.setTelephone(tv_enterprise_telephone.getText().toString());
                switch (tv_enterprise_scale.getSelectedItemPosition()) {
                    case 0:
                        RegisterActivity.company.setScale(49);
                        break;
                    case 1:
                        RegisterActivity.company.setScale(99);
                        break;
                    case 2:
                        RegisterActivity.company.setScale(499);
                        break;
                    case 3:
                        RegisterActivity.company.setScale(1000);
                    default:
                        RegisterActivity.company.setScale(49);
                }
                RegisterActivity.company.setDepartments(departmentAdapter.getDepartmentList());
                message(null);
            }
        });

    }

}
